//
//  UIBarButtonItem+init.m
//  SinaVboc
//
//  Created by V on 29/11/2016.
//  Copyright © 2016 仁忄. All rights reserved.
//

#import "UIBarButtonItem+init.h"

@implementation UIBarButtonItem (init)

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName target:(id)target action:(SEL)action {
    
    if ([self init]) {
        
        UIButton *btn = [[UIButton alloc] init];
        
        [btn setTitle:title forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:18];
        
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
        
        
        if (imageName) {
            
            [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_highlighted", imageName]] forState:UIControlStateHighlighted];

        }
        
        [btn sizeToFit];
        
        if (action) {
            
            [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        }
        
        self = [[UIBarButtonItem alloc] initWithCustomView:btn];
    }
    
    return  self;
}

@end
